// services/logistic.js
const { request, requestWithLoading, uploadFile } = require('../utils/request');

function searchLogistic(params = {}) {
  return requestWithLoading({
    url: '/logistic/search',
    method: 'POST',
    data: params,
    loadingText: '查询中...'
  });
}

function checkOrder(params = {}) {
  return requestWithLoading({
    url: '/logistic/check_order',
    method: 'POST',
    data: params,
    loadingText: '校验中...'
  });
}

function checkOrderForIdCard(params = {}) {
  return requestWithLoading({
    url: '/order/check_order',
    method: 'POST',
    data: params,
    loadingText: '验证订单号中...'
  });
}

function uploadIdCard(params = {}) {
  const { file, type, order_id, ...otherParams } = params;
  
  // 验证必要参数
  if (!file) {
    return Promise.reject({ message: '文件路径不能为空' });
  }
  
  if (!type) {
    return Promise.reject({ message: '上传类型不能为空' });
  }
  
  if (!order_id) {
    return Promise.reject({ message: '订单ID不能为空' });
  }
  
  // 使用文件上传方法
  // 注意：uploadFile 会自动设置 Content-Type: multipart/form-data
  return uploadFile({
    url: '/index/upload',
    filePath: file,
    name: 'file',
    formData: {
      type: String(type),
      order_id: String(order_id),
      ...otherParams
    },
    loadingText: '上传中...'
  });
}

function submitIdCardUpload(params = {}) {
  return requestWithLoading({
    url: '/order/upload_idcard',
    method: 'POST',
    data: params,
    loadingText: '提交中...'
  });
}

module.exports = {
  searchLogistic,
  checkOrder,
  checkOrderForIdCard,
  uploadIdCard,
  submitIdCardUpload
};


